package o;

import android.app.PendingIntent;
import android.content.Context;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.foundation.store.bean.spilt.GetApksInfoRequest;
import com.huawei.appgallery.foundation.store.bean.spilt.GetApksInfoRequest.b;
import com.huawei.appgallery.foundation.store.bean.spilt.GetApksInfoResponse;
import com.huawei.appgallery.foundation.store.bean.spilt.GetApksInfoResponse.ModuleInfo;
import com.huawei.appgallery.foundation.store.bean.spilt.GetApksInfoResponse.SplitApkInfo;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import java.util.ArrayList;
import java.util.List;

public class bpa extends cgg<GetApksInfoRequest, GetApksInfoResponse> implements bpm, bpj {
    private static volatile long ˏ = -1;
    private int ʻ;
    private String ʼ;
    private ArrayList<Integer> ʽ = new ArrayList();
    private final bpx ˊ;
    private final Context ˋ;
    private bpg ˋॱ;
    private String ˎ;
    private bqf ॱ;
    private boolean ॱˊ;
    private ArrayList<String> ॱॱ = new ArrayList();
    private ArrayList<String> ᐝ = new ArrayList();

    protected /* synthetic */ void ˋ(@NonNull BaseRequest o_cvu, @NonNull ResponseBean responseBean) {
        ॱ((GetApksInfoRequest) o_cvu, (GetApksInfoResponse) responseBean);
    }

    public bpa(@NonNull Context context, @NonNull bpx o_bpx) {
        this.ˋ = context;
        this.ˊ = o_bpx;
    }

    public void ˋ(boolean z) {
        if (z) {
            ˊ();
        } else {
            this.ˊ.ॱ(5, "Not signed the agreement.");
        }
    }

    public void ˊ(boolean z, @Nullable Object obj) {
        if (z) {
            ˏ();
        } else {
            this.ˊ.ॱ(6, "The installation has been canceled.");
        }
    }

    public void ˋ(@NonNull String str, @NonNull bqf o_bqf) {
        boc.ˎ.ˋ("StartInstallHandler", "startInstall, pkgName: " + str + ", modules: " + o_bqf);
        this.ˎ = str;
        this.ॱ = o_bqf;
        this.ʻ = bla.ॱ(this.ˋ, this.ˎ);
        this.ʼ = new bpb().ˏ(this.ˎ).ॱ(this.ʻ).ˊ(this.ॱ).ˏ(10).ˏ();
        if (!ccx.ॱ()) {
            boc.ˎ.ˏ("StartInstallHandler", "Require to sign the agreement.");
            bpp.ˏ().ˏ(this);
            new bpq(this.ˋ, this.ˎ).ˎ();
        } else if (!ॱ()) {
            ˊ();
        }
    }

    private boolean ॱ() {
        bol ˊ = bor.ˎ().ˊ(this.ʼ);
        if (ˊ == null) {
            return false;
        }
        boc.ˎ.ˋ("StartInstallHandler", "Session already exists.");
        int ˏ = ˊ.ˏ();
        this.ˊ.ˊ(ˏ);
        if (ˊ.ʻ() == 11) {
            ArrayList ˏॱ = ˊ.ˏॱ();
            bqd ͺ = ˊ.ͺ();
            if (!(ˏॱ == null || ͺ == null)) {
                new bpq(this.ˋ, this.ˎ).ˏ(ˏ, ˏॱ, ͺ);
            }
        }
        return true;
    }

    private void ˊ() {
        if (!bky.ˎ(this.ˋ)) {
            this.ˊ.ॱ(9, "No connected network.");
        } else if (ˏ >= 0) {
            ˎ();
        } else {
            fjo ˏ = bof.ˊ().ˏ();
            if (ˏ == null) {
                boc.ˎ.ˏ("StartInstallHandler", "Return null when calling 'getLimitSize'.");
                ˎ();
                return;
            }
            ˏ.ˏ(new fjq<Long>(this) {
                final /* synthetic */ bpa ॱ;

                {
                    this.ॱ = r1;
                }

                public void onComplete(fjo<Long> o_fjo_java_lang_Long) {
                    boc.ˎ.ˋ("StartInstallHandler", "onComplete for calling 'getLimitSize'.");
                    if (o_fjo_java_lang_Long.ˊ()) {
                        bpa.ˏ = ((Long) o_fjo_java_lang_Long.ॱ()).longValue();
                        boc.ˎ.ˋ("StartInstallHandler", "Set sLimitSize: " + bpa.ˏ);
                    }
                    this.ॱ.ˎ();
                }
            });
        }
    }

    private void ˎ() {
        if (TextUtils.isEmpty(this.ˎ) || this.ॱ == null || this.ॱ.ˋ().isEmpty()) {
            boc.ˎ.ˊ("StartInstallHandler", "Run to unreachable logic.");
        } else if (bky.ˎ(this.ˋ)) {
            b ˊ = new b(this.ˋ).ˎ(this.ˎ).ˊ(this.ʻ);
            for (bqf.b bVar : this.ॱ.ˋ()) {
                ˊ.ˊ(bVar.ˎ(), bVar.ˋ());
            }
            cvt ॱ = ˊ.ॱ();
            if (ॱ.ॱ()) {
                this.ˊ.ॱ(10, "Already installed.");
            } else {
                bqe.ˋ(ॱ, this);
            }
        } else {
            this.ˊ.ॱ(9, "No connected network.");
        }
    }

    protected void ॱ(@NonNull GetApksInfoRequest getApksInfoRequest, @NonNull GetApksInfoResponse getApksInfoResponse) {
        if (getApksInfoResponse.ˏ()) {
            List<ModuleInfo> ˊ = getApksInfoResponse.ˊ();
            if (ˊ == null || ˊ.isEmpty()) {
                this.ˊ.ॱ(100, "Empty module-infos.");
                return;
            }
            bpg ˊ2 = new bpg().ˏ(this.ˎ).ˊ(getApksInfoRequest.ˏ());
            long j = 0;
            for (ModuleInfo moduleInfo : ˊ) {
                List<SplitApkInfo> ˊ3 = moduleInfo.ˊ();
                if (ˊ3 == null || ˊ3.isEmpty()) {
                    this.ˊ.ॱ(100, "Empty splitApk-infos.");
                    return;
                }
                for (SplitApkInfo splitApkInfo : ˊ3) {
                    long ˊ4 = splitApkInfo.ˊ();
                    if (ˊ4 <= 0) {
                        this.ˊ.ॱ(100, "Invalid splitApk-size.");
                        return;
                    } else {
                        ˊ2.ˋ(new bpf().ˏ(moduleInfo.ॱ(), moduleInfo.ˏ()).ˊ(splitApkInfo));
                        j += ˊ4;
                    }
                }
                if ("language".equals(moduleInfo.ॱ())) {
                    this.ॱॱ.add(moduleInfo.ˎ());
                } else {
                    int ˋ = moduleInfo.ˋ();
                    if (ˋ == 0) {
                        this.ˊ.ॱ(100, "Invalid splitApk-featureTitle.");
                        return;
                    } else {
                        this.ʽ.add(Integer.valueOf(ˋ));
                        this.ᐝ.add(moduleInfo.ˎ());
                    }
                }
            }
            this.ˋॱ = ˊ2;
            Object obj = (bpo.ˋ(this.ˋ) || (bpo.ˊ(this.ˋ) && bpo.ˏ(this.ˋ))) ? 1 : null;
            if (j <= ˏ || obj == null) {
                ˏ();
                return;
            }
            PendingIntent ˎ = new bpl(this.ˋ, this.ˎ, this).ˎ(j, this.ʽ, !this.ॱॱ.isEmpty());
            if (ˎ == null) {
                this.ˊ.ॱ(1, "Fail to create 'PendingIntent' for 'InstallConfirmActivity'.");
                return;
            }
            new bpq(this.ˋ, this.ˎ).ˋ(ˎ);
            this.ॱˊ = true;
            return;
        }
        this.ˊ.ॱ(100, "Fail to request '" + getApksInfoRequest.getMethod_() + " || responseCode: " + getApksInfoResponse.getResponseCode() + ", rtnCode_: " + getApksInfoResponse.getRtnCode_());
    }

    private void ˏ() {
        if (this.ˋॱ == null) {
            boc.ˎ.ˊ("StartInstallHandler", "Run to unreachable logic.");
        } else if (!ॱ()) {
            SessionDownloadTask ˎ = this.ˋॱ.ˎ();
            bol o_bol = new bol(this.ˎ, ˎ.ʾ());
            o_bol.ˊ(this.ʼ);
            o_bol.ˎ(this.ʽ);
            o_bol.ˋ(this.ᐝ);
            o_bol.ˏ(this.ॱॱ);
            if (this.ॱ != null) {
                for (bqf.b bVar : this.ॱ.ˋ()) {
                    o_bol.ˏ(bVar.ˎ(), bVar.ˋ());
                }
            }
            o_bol.ˎ(true);
            bor.ˎ().ॱ(o_bol);
            List ॱˎ = o_bol.ॱˎ();
            ˎ.ˏ(this.ॱˊ);
            ˎ.ʻॱ(bks.ˊ((Integer[]) ॱˎ.toArray(new Integer[0]), ","));
            bof.ˏ().ˊ(ˎ);
            this.ˊ.ˊ(o_bol.ˏ());
        }
    }
}
